package cn.com.aiouyi.controller.admin;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.Accessory;
import cn.com.aiouyi.entity.Article;
import cn.com.aiouyi.entity.ArticleClass;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.AccessoryService;
import cn.com.aiouyi.service.ArticleClassService;
import cn.com.aiouyi.service.ArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tommy on 2018/4/26.
 */
@Controller("adminArticleController")
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {

    @Resource(name = "articleServiceImpl")
    private ArticleService articleService;

    @Resource(name = "articleClassServiceImpl")
    private ArticleClassService articleClassService;

    @Resource(name = "accessoryServiceImpl")
    private AccessoryService accessoryService;

    @GetMapping("/list")
    public String list(Pageable pageable, ModelMap model) {

        model.put("page", articleService.findPage(pageable));
        return "/admin/article/list";
    }

    @GetMapping("/add")
    public String add(ModelMap model) {
        model.put("articleClasses", articleClassService.findAll());
        return "/admin/article/add";
    }

    @PostMapping("/save")
    public String save(@Valid Article article, Long[] articleClassIds, Long fileId, RedirectAttributes redirectAttributes) {
        if (articleClassIds.length == 0) {
            addFlashMessage(redirectAttributes, Message.error("分类不能为空"));
            return "redirect:list";
        }
        List<ArticleClass> articleClasses = new ArrayList<>();
        for (int i = 0; i < articleClassIds.length; i++) {
            ArticleClass articleClass = articleClassService.find(articleClassIds[i]);
            articleClasses.add(articleClass);
        }
        if (articleClasses.size() == 0) {
            addFlashMessage(redirectAttributes, Message.error("分类不存在"));
            return "redirect:list";
        }
        if (fileId != null){
            Accessory accessory = accessoryService.find(fileId);
            if (accessory!=null){
                article.setMainPic(accessory);
            }
        }
        article.setArticleClasses(articleClasses);
        articleService.save(article);
        addFlashMessage(redirectAttributes, Message.SUCCESS);
        return "redirect:list";
    }

    @GetMapping("/edit")
    public String edit(Long id, ModelMap model) {
        model.put("articleClasses", articleClassService.findAll());
        model.put("article", articleService.find(id));

        List<ArticleClass> acs = articleClassService.findAll();
        ArticleClass ac = articleClassService.find(1L);
        if (acs.contains(ac)){
            logger.info("yes!!!");
        }
        return "/admin/article/edit";
    }

    @PostMapping("/update")
    public String update(@Valid Article article, Long[] articleClassIds, Long fileId,
                         ModelMap model, RedirectAttributes redirectAttributes) {
        if (articleClassIds == null) {
            addFlashMessage(redirectAttributes, Message.error("分类不能为空"));
            return "redirect:list";
        }
        List<ArticleClass> articleClasses = new ArrayList<>();
        for (int i = 0; i < articleClassIds.length; i++) {
            ArticleClass articleClass = articleClassService.find(articleClassIds[i]);
            articleClasses.add(articleClass);
        }
        if (articleClasses.size() == 0) {
            addFlashMessage(redirectAttributes, Message.error("分类不存在"));
            return "redirect:list";
        }
        if (fileId != null){
            Accessory accessory = accessoryService.find(fileId);
            if (accessory!=null){
                article.setMainPic(accessory);
            }
        }
        article.setArticleClasses(articleClasses);
        articleService.update(article);
        addFlashMessage(redirectAttributes, Message.SUCCESS);
        return "redirect:list";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Message delete(Long[] ids, HttpServletRequest request, Model model){
        if (ids != null && ids.length >0){
            articleService.delete(ids);
            return Message.SUCCESS;
        } else {
            model.addAttribute(ERROR_VIEW_MESSAGE_KEY, "id不能为空");
            return Message.ERROR;
        }
    }
}
